<?php

class StudentHallOfFame extends Controller {

	function StudentHallOfFame()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		$this->load->view('header');
		$this->load->view('hall-detail-form');
		$this->load->view('footer');
	}
	
	function inFrame()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		$this->load->view('header');
		$this->load->view('hall-in-detail-form');
		$this->load->view('footer');
	}
	
	function outFrame()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		$this->load->view('header');
		$this->load->view('hall-out-detail-form');
		$this->load->view('footer');
	}
	
	function detailIn()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('studenthalloffamemodel');
	    $newsID = $this->input->post('txtId'); 
	    if($newsID != null) {
			$data = $this->studenthalloffamemodel->getByID($newsID);		
			$this->load->view('header');
			$this->load->view('hall-in-detail-form', $data);
			$this->load->view('footer');	    	
	    }     
	}
	
	function detailOut()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('studenthalloffamemodel');
	    $newsID = $this->input->post('txtId'); 
	    if($newsID != null) {
			$data = $this->studenthalloffamemodel->getByID($newsID);		
			$this->load->view('header');
			$this->load->view('hall-out-detail-form', $data);
			$this->load->view('footer');	    	
	    }     
	}
	
	function detail()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('studenthalloffamemodel');
	    $newsID = $this->input->post('txtId'); 
	    if($newsID != null) {
			$data = $this->studenthalloffamemodel->getByID($newsID);		
			$this->load->view('header');
			$this->load->view('hall-detail-form', $data);
			$this->load->view('footer');	    	
	    }     
	}
	
	function lists()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('studenthalloffamemodel');
	    $data['data'] = $this->studenthalloffamemodel->getAll();
	    $this->load->view('header');
		$this->load->view('hall-list-form', $data);
		$this->load->view('footer');
	    
	}
	
	function delete()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('studenthalloffamemodel');
	    $newsID = $this->input->post('txtId'); 
	    if($newsID != null) {
			$data = $this->studenthalloffamemodel->delete($newsID);				
	    }
	    $this->lists();
	}
	
	function deletes(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('studenthalloffamemodel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$this->studenthalloffamemodel->delete($id[$i]);
	    }	    
	    $this->lists();
	}
	
	function saveInFrame()
	{
		$this->load->model('studenthalloffamemodel');
		
		$data = array();
		$data['show'] =  'Y';
		$data['detail'] =  $this->input->post('txtDetail') ? $this->input->post('txtDetail') : null;
		
        if($this->input->post('txtId') != null && $this->input->post('txtId') != '') {
        	$data['stu_id'] = $this->input->post('txtId');
        	$key = $this->input->post('txtId');
        	$this->studenthalloffamemodel->update($data);
        } else {
			$key = $this->studenthalloffamemodel->add($data);
        }
        $msg = array('msg' => '');
        if(isset($_FILES))
        {
        	if(@$_FILES['imageUpload']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/hall/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = $key.'-image';
				$this->load->library('upload', $config);
				
				if ( ! $this->upload->do_upload('imageUpload'))
				{
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์รูปภาพได้";			
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'stu_id' => $key, 
		               'image' => $data['upload_data']['file_name']   
		            );
					$this->studenthalloffamemodel->updateImagePath($Imgdata);
				}
        	}
        	if($msg['msg'] == '') 
        	{
        		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        	} 
        } else {
        	$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        }
		
		$data = $this->studenthalloffamemodel->getByID($key);		
		$this->load->view('header');
		$this->load->view('hall-in-detail-form', $data);
		$this->load->view('footer', $msg);
	}
	
	function saveOutFrame()
	{
		$this->load->model('studenthalloffamemodel');
		
		$data = array();
		$data['show'] =  'N';
		$data['name'] =$this->input->post('txtName') ? $this->input->post('txtName') : null; 
		$data['nick_name'] =$this->input->post('txtNickName') ? $this->input->post('txtNickName') : null;
		$data['school_name'] =  $this->input->post('txtSchoolName') ? $this->input->post('txtSchoolName') : null;
		$data['current_class'] =  $this->input->post('txtCurrentClass') ? $this->input->post('txtCurrentClass') : null;
		$data['tutor_name'] =  $this->input->post('txtTutorName') ? $this->input->post('txtTutorName') : null;
		$data['detail'] =  $this->input->post('txtDetail') ? $this->input->post('txtDetail') : null;
		
        if($this->input->post('txtId') != null && $this->input->post('txtId') != '') {
        	$data['stu_id'] = $this->input->post('txtId');
        	$key = $this->input->post('txtId');
        	$this->studenthalloffamemodel->update($data);
        } else {
			$key = $this->studenthalloffamemodel->add($data);
        }
        $msg = array('msg' => 'บันทึกข้อมูลเรียบร้อย');
		$data = $this->studenthalloffamemodel->getByID($key);		
		$this->load->view('header');
		$this->load->view('hall-out-detail-form', $data);
		$this->load->view('footer', $msg);
	}
	
	function upload()
	{
		$this->load->model('studenthalloffamemodel');
		
		$data = array();
		$data['name'] =$this->input->post('txtName') ? $this->input->post('txtName') : null; 
		$data['nick_name'] =$this->input->post('txtNickName') ? $this->input->post('txtNickName') : null;
		$data['school_name'] =  $this->input->post('txtSchoolName') ? $this->input->post('txtSchoolName') : null;
		$data['current_class'] =  $this->input->post('txtCurrentClass') ? $this->input->post('txtCurrentClass') : null;
		$data['tutor_name'] =  $this->input->post('txtTutorName') ? $this->input->post('txtTutorName') : null;
		$data['show'] =  $this->input->post('txtTopShow') ? $this->input->post('txtTopShow') : 'N';
		$data['detail'] =  $this->input->post('txtDetail') ? $this->input->post('txtDetail') : null;
		
        if($this->input->post('txtId') != null && $this->input->post('txtId') != '') {
        	$data['stu_id'] = $this->input->post('txtId');
        	$key = $this->input->post('txtId');
        	$this->studenthalloffamemodel->update($data);
        } else {
			$key = $this->studenthalloffamemodel->add($data);
        }
        $msg = array('msg' => '');
        if(isset($_FILES))
        {
        	if(@$_FILES['imageUpload']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/hall/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = $key.'-image';
				$this->load->library('upload', $config);
				
				if ( ! $this->upload->do_upload('imageUpload'))
				{
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์รูปภาพได้";			
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'stu_id' => $key, 
		               'image' => $data['upload_data']['file_name']   
		            );
					$this->studenthalloffamemodel->updateImagePath($Imgdata);
				}
        	}
        	if($msg['msg'] == '') 
        	{
        		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        	} 
        } else {
        	$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        }
		
		$data = $this->studenthalloffamemodel->getByID($key);		
		$this->load->view('header');
		$this->load->view('hall-detail-form', $data);
		$this->load->view('footer', $msg);
	}
}

